#include "learnermodel.h"

namespace IAlib
{

LearnerModel::LearnerModel(): m_id(""), m_role("")
{
}

LearnerModel::LearnerModel(const LearnerModel &model)
{
    m_id = model.m_id;
    m_role = model.m_role;
    for (int i = 0; i < model.m_its_actions.size(); i++)
    {
        addAction(*model.m_its_actions[i]);
    }
    m_performed_missions = model.m_performed_missions;
}

LearnerModel::~LearnerModel()
{
    for (int i = 0; i < m_its_actions.size(); i++ )
    {
        delete(m_its_actions[i]);
    }
}

void LearnerModel::addAction(const ItsAction &its_action)
{
    ItsAction * action = new ItsAction(its_action);
    m_its_actions.append(action);
    emit newAction(m_id);
}

void LearnerModel::addKnowledgeBase(Knowledges *kw)
{
    emit newKnowledge(m_id);
}

void LearnerModel::removeAction(const int index)
{
    if (index >= 0 && index < m_its_actions.size())
    {
        delete (m_its_actions[index]);
        m_its_actions.removeAt(index);
    }
}

void LearnerModel::addPerformedMission(const QString &mission_name)
{
    m_performed_missions.append(mission_name);
}

/*void LearnerModel::removeAssignedMission(const QString &mission_name)
{
    bool exist = false;
    int i = 0;
    for (int i = 0; i < m_assigned_missions.size(); i++)
    {
        if (m_assigned_missions[i]->getName() == mission_name)
        {
            exist = true;
            break;
        }
    }
    if (exist)
    {
        delete(m_assigned_missions[i]);
        m_assigned_missions.removeAt(i);
    }
}*/

ItsAction * LearnerModel::getAction(const int index)
{
    if (index >= 0 && index < m_its_actions.size())
    {
        return m_its_actions[index];
    }
    else
    {
        return NULL;
    }
}

QDomElement LearnerModel::toXML(QDomDocument &doc)
{
    QDomElement ele = doc.createElement("learnerModel");
    ele.setAttribute("id", m_id);
    QDomElement missions = doc.createElement("assignedMission");
    /*for (int i = 0; i < m_assigned_missions.size(); i++)
    {
        missions.appendChild(m_assigned_missions[i]->toXML(doc));
    }*/
    ele.appendChild(missions);
    QDomElement actions = doc.createElement("ItsActions");
    for (int i = 0; i < m_its_actions.size(); i++)
    {
        actions.appendChild(m_its_actions[i]->toXML(doc));
    }
    ele.appendChild(actions);
    return ele;
}

void LearnerModel::fromXML(QDomElement ele)
{
    m_id = ele.attribute("id", "");
    QDomNode n = ele.firstChild();
    while (!n.isNull())
    {
        QDomElement e = n.toElement();
        if (e.tagName() == "ItsActions")
        {
            QDomNode actionNode = e.firstChild();
            while (!actionNode.isNull())
            {
                ItsAction action;
                action.fromXML(actionNode.toElement());
                addAction(action);
                actionNode = actionNode.nextSibling();
            }
        }
        else if (e.tagName() == "assignedMission")
        {
          /*  QDomNode missionNode = e.firstChild();
            while (!missionNode.isNull())
            {
                Mission mission;
                mission.fromXML(missionNode.toElement());
                addAssignedMission(mission);
                missionNode = missionNode.nextSibling();
            }*/
        }
        n = n.nextSibling();
    }
}

int LearnerModel::hasMission(const QString &mission_name) const
{
    for (int i = 0; i < m_performed_missions.size(); i++)
    {
        if (m_performed_missions[i] == mission_name)
        {
            return i;
        }
    }
    return -1;
}

LearnerModel& LearnerModel::operator=(const LearnerModel& model)
{
    m_id = model.m_id;
    m_role = model.m_role;
    for (int i = 0; i < m_its_actions.size(); i++ )
    {
        delete(m_its_actions[i]);
    }
    m_its_actions.clear();
    m_performed_missions = model.m_performed_missions;
    for (int i = 0; i < model.m_its_actions.size(); i++)
    {
        addAction(*model.m_its_actions[i]);
    }
    return *this;
}

}
